package com.tbm.console.proj.service.impl;

import com.tbm.console.proj.entity.SystemMenu;
import com.tbm.console.proj.repository.SystemMenuRepository;
import com.tbm.console.proj.service.SystemMenuService;
import com.tbm.console.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

@Service
public class SystemMenuServiceImpl implements SystemMenuService {

    @Autowired
    private SystemMenuRepository systemMenuRepository;


    @Override
    public List<SystemMenu> findAll() {
        return findAll(new PageData());
    }

    @Override
    public List<SystemMenu> findAll(Integer start, Integer length) {
        return findAll(new PageData(),start,length);
    }

    @Override
    public List<SystemMenu> findAll(PageData pageData) {
//        Sort sort = new Sort(Sort.Direction.ASC, "menuOrder");
        Specification<SystemMenu> sp = this.getSystemMenuSpecification(pageData);
        List<SystemMenu> all = systemMenuRepository.findAll(sp);
        return all;
    }

    @Override
    public List<SystemMenu> findAll(PageData pageData, Integer start, Integer length) {
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(start, length, sort);
        Specification<SystemMenu> sp = this.getSystemMenuSpecification(pageData);
        List<SystemMenu> all = systemMenuRepository.findAll(sp,pageable).getContent();
        return all;
    }


    @Override
    public SystemMenu findById(Integer id) {
        SystemMenu systemMenu = new SystemMenu();
        try{
            systemMenu = systemMenuRepository.findById(id).get();
        }catch (Exception e){

        }
        return systemMenu;
    }


    @Override
    public long count() {
        return count(new PageData());
    }

    @Override
    public long count(SystemMenu systemMenu) {
        Example<SystemMenu> exa = Example.of(systemMenu);
        return systemMenuRepository.count(exa);
    }

    @Override
    public long count(PageData pageData) {
        Specification<SystemMenu> sp = this.getSystemMenuSpecification(pageData);
        return systemMenuRepository.count(sp);
    }

    @Override
    public boolean deleteById(Integer id) {
        systemMenuRepository.deleteById(id);
        return true;
    }

    @Override
    public boolean deleteByIds(Iterable<SystemMenu> ids) {
        return false;
    }

    @Override
    public SystemMenu save(SystemMenu systemMenu) {
        List<SystemMenu> list = saveAll(Arrays.asList(systemMenu));
        if(list.size()==0){
            return new SystemMenu();
        }
        return list.get(0);
    }

    @Override
    public List<SystemMenu> saveAll(List<SystemMenu> systemMenus) {
        return systemMenuRepository.saveAll(systemMenus);
    }

    @Override
    public List<SystemMenu> findAllById(Integer [] ids) {
        Iterable<Integer> iterable = new Iterable<Integer>() {
            @Override
            public Iterator<Integer> iterator() {
                List<Integer> idsList = Arrays.asList(ids);
                return idsList.iterator();
            }
        };
        return systemMenuRepository.findAllById(Arrays.asList(ids));
    }

    //通过Specification来获取自定义的查询条件
    private Specification<SystemMenu> getSystemMenuSpecification(PageData pageData){
        return new Specification() {
            @Override
            public javax.persistence.criteria.Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> preList = new ArrayList<Predicate>();
                if (null != pageData.get("-")) {
                    preList.add(criteriaBuilder.greaterThan(root.get("-"), pageData.get("-").toString()));
                }
                if (null != pageData.get("-")) {
                    preList.add(criteriaBuilder.lessThan(root.get("-"), pageData.get("-").toString()));
                }
                if (null != pageData.get("parentId")) {
                    preList.add(criteriaBuilder.equal(root.get("parentId"), pageData.get("parentId").toString()));
                }
                if (null != pageData.get("deleteFlag")) {
                    preList.add(criteriaBuilder.equal(root.get("deleteFlag"), pageData.get("deleteFlag").toString()));
                }
                if (null != pageData.get("menuName")) {
                    preList.add(criteriaBuilder.equal(root.get("menuName"), pageData.get("menuName").toString()));
                }
                if (null != pageData.get("menuType")) {
                    preList.add(criteriaBuilder.equal(root.get("menuType"), pageData.get("menuType").toString()));
                }

                Predicate[] p = new Predicate[preList.size()];
                return criteriaBuilder.and(preList.toArray(p));
            }
        };
    }
}
